//
// Created by 32827 on 2024/7/23.
//
#include "yzl_spi_ad7606.h"

YZL_GPIO ad7606_cs={YZF_PIN_6,GPIOD},ad7606_miso={YZF_PIN_4,GPIOB},ad7606_mosi={YZF_PIN_7,GPIOA},ad7606_clk={YZF_PIN_5,GPIOA};
YZL_GPIO ad7606_conv={YZF_PIN_3,GPIOA},ad7606_rst={YZF_PIN_4,GPIOA};
YZL_GPIO ad7606_busy={YZF_PIN_1,GPIOC},ad7606_fd={YZF_PIN_2,GPIOC};
uint16_t yzl_spi_ad7606_read_once(){
    YLGPIO(ad7606_conv)
    yzf_gpio_open_clock_();
    yzf_gpio_set_PP_();
    yzf_gpio_set_HIGH_();
    yzf_gpio_set_1_();

    YLGPIO(ad7606_rst)
    yzf_gpio_open_clock_();
    yzf_gpio_set_0_();
    yzf_gpio_set_PP_();
    yzf_gpio_set_HIGH_();

    YLGPIO(ad7606_busy)
    yzf_gpio_open_clock_();
    yzf_gpio_set_IN_PULL_UP_();
    yzf_gpio_set_HIGH_();
    yzf_gpio_set_1_();

    YLGPIO(ad7606_fd)
    yzf_gpio_open_clock_();
    yzf_gpio_set_IN_PULL_UP_();
    yzf_gpio_set_HIGH_();
    yzf_gpio_set_1_();


    YLGPIO(ad7606_cs)
    yzf_gpio_open_clock_();
    yzf_gpio_set_PP_();
    yzf_gpio_set_Pull_Up_();
    yzf_gpio_set_HIGH_();
    yzf_gpio_set_1_();
    YLGPIO(ad7606_miso)
    yzf_gpio_open_clock_();
    yzf_gpio_set_AFPP_();
    yzf_gpio_set_HIGH_();
    yzf_gpio_set_1_();
    yzf_gpio_set_af_(5);
    YLGPIO(ad7606_mosi)
    yzf_gpio_open_clock_();
    yzf_gpio_set_AFPP_();
    yzf_gpio_set_HIGH_();
    yzf_gpio_set_1_();
    yzf_gpio_set_af_(5);
    YLGPIO(ad7606_clk)
    yzf_gpio_open_clock_();
    yzf_gpio_set_AFPP_();
    yzf_gpio_set_HIGH_();
    yzf_gpio_set_1_();
    yzf_gpio_set_af_(5);

    yz_spi_init16_full_lines_ex(SPI1,YZF_SPI_PLCK_256, YZF_TRUE,YZF_TRUE);
    yz_spi_recive_transmit_full_lines_start(SPI1);
    YLGPIO(ad7606_rst)
    yzf_gpio_set_1_();
    yzl_softport_delayUS(20);
    yzf_gpio_set_0_();
    YLGPIO(ad7606_conv)
    yzl_softport_delayUS(10);
    yzf_gpio_set_1_();
    yzl_softport_delayUS(50);
    yzf_gpio_set_0_();
    yzl_softport_delayUS(50);
    yzf_gpio_set_1_();

    yzl_softport_delayUS(50);
    YLGPIO(ad7606_busy)
    while (yzf_gpio_get_());

    YLGPIO(ad7606_cs)

    yzf_gpio_set_0_();
    uint16_t data=yz_spi_recive16_transmit_full_lines(SPI1,0xFFFF);
    YLGPIO(ad7606_cs)
    yzf_gpio_set_1_();
    yz_spi_recive_transmit_full_lines_end(SPI1);
    return data;
}